﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using GraphDrawing2.Canvas;
using GraphDrawing2.Core;
using GraphDrawing2.V2.Concrete.Model;
using GraphDrawing2.V2.Concrete.Renderers;
using GraphDrawing2.V2.Graph.Model;

namespace GraphDrawing2.V2
{
    public partial class Form1 : Form, IMyView
    {
        readonly MutableRendererFactory factory = new MutableRendererFactory();

        public Form1()
        {
            InitializeComponent();

            var br = new BackgroundView();
            br.Background.Color = Color.FloralWhite;
            br.Grid.Color = Color.FromArgb(192, Color.Black);
            br.Cell.Color = Color.FromArgb(192, Color.Gray);
            br.Cell.DashStyle = DashStyle.Dash;
            factory.SetBackgroundRenderer(br);
        }

        public void Initialize(MyModel model)
        {
            var cmodel = canvasView1.Controller.Model;

            var background = new BackgroundRenderingPresenter(cmodel.ViewPort, factory, 256);

            canvasView1.Controller.Model.AddPainter(PaintStage.BACKGROUND, background);
            canvasView1.Controller.Model.AddPainter(PaintStage.CONTENT, new BoxPainter(model.Graph, factory));

            var main = new CanvasListener(canvasView1.Controller.Model, model.Graph, canvasView1);
            canvasView1.Controller.Model.Listeners.Add(main);

            model.Graph.Add(new Shape());
            model.Graph.Add(new ConnectableElement());
        }

        class BoxPainter : IPainter
        {
            readonly ElementsModel model;

            public BoxPainter(ElementsModel model, IRendererFactory factory)
            {
                this.model = model;
            }

            public void Draw(DrawingContext dc)
            {
                foreach (IRenderable box in model.GetElements())
                    box.Render(dc);
            }
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            canvasView1.Controller.Model.ViewPort.ZoomIn();
            canvasView1.UpdateView();
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            canvasView1.Controller.Model.ViewPort.ZoomOut();
            canvasView1.UpdateView();
        }
    }
}
